
#pragma once

#include <functional>

static inline std::string match(const std::string& line, const umap_ss& keywords)
{
    for (const auto& kw : keywords)
    {
        if (line.find(kw.second) != std::string::npos)
        {
            return kw.first;
        }
    }
    return "";
}

static inline void read_lines(const char* filepath, int64_t& fileOffset, std::function<void(std::string&)> line_func)
{
    std::error_code  error;
    mio::mmap_source mmap = mio::make_mmap_source(filepath, 0, mio::map_entire_file, error);

    if (fileOffset > (long)mmap.size())
        fileOffset = 0;
    do
    {
        auto it = std::find_if(mmap.begin() + fileOffset, mmap.end(), [](uint8_t c) {
            return c == (uint8_t)'\n';
        });

        if (it != mmap.end())
        {
            std::string line = std::string(mmap.begin() + fileOffset, it);

            line_func(line);

            fileOffset += line.size() + 1;
            continue;
        }
        break;
    } while (true);

    mmap.unmap();
}
